package jieKeXiangMu;

import java.io.*;
import java.util.*;
public class Dormitory {
	
	public void student() {
		System.out.println("* 录入学生信息：*");
		File f1 = new File("D:\\计算机\\大二上\\文件\\Pro6\\src\\jieKeXiangMu\\录入.txt");
		String studentName;
		int studentID;
		try {
			Scanner sc = new Scanner(System.in);
			OutputStream fo = new FileOutputStream(f1);
		    OutputStreamWriter ro = new OutputStreamWriter(fo);
		    BufferedWriter br1 = new BufferedWriter(ro);
		    
			System.out.println("请输入学生姓名：");
			studentName = sc.nextLine();
			System.out.println("请输入学生学号：");
			studentID = sc.nextInt();
			
			br1.write(studentName + " " + studentID);
		    br1.close();
		    System.out.println("录入成功~");
		}catch(Exception exp) {
			System.out.println(exp);
		}
	}
	
	
	
	public void inquiry() {
		System.out.println("* 信息查询：*");
		File f2 = new File("D:\\计算机\\大二上\\文件\\Pro6\\src\\jieKeXiangMu\\查询.txt");
		try {
			FileInputStream fi2 = new FileInputStream(f2);
		    InputStreamReader ir2 = new InputStreamReader(fi2);
	        BufferedReader br2 = new BufferedReader(ir2);
	        String line2;
	        while((line2=br2.readLine())!=null) {
	        	System.out.println(line2);
	        }
	        br2.close();
	     }catch(Exception exp) {
				System.out.println(exp);
		 }
	}
	
	
	
	public void accommodation() {
		System.out.println("* 安排学生入住：*");
		System.out.println("系统自动安排宿舍床位中~");
		File f = new File("D:\\计算机\\大二上\\文件\\Pro6\\src\\jieKeXiangMu\\宿舍资源.txt");
		File f1 = new File("D:\\计算机\\大二上\\文件\\Pro6\\src\\jieKeXiangMu\\录入.txt");
		File f0 = new File("D:\\计算机\\大二上\\文件\\Pro6\\src\\jieKeXiangMu\\宿舍资源(缓存).txt");
		try {
			String studentAccommodation;
			Scanner sc = new Scanner(f);
			Scanner sc3 = new Scanner(f1);
			studentAccommodation = sc3.nextLine();
			sc3.close();
			
			List<String> lines = new ArrayList<>();
			String line;
			int index = -1;
			while(sc.hasNext()) {
				line = sc.nextLine();
				lines.add(line);
				if(index == -1) {
					if(line.contains("空")) {
						index = lines.size() - 1;
					}
				}
			}
			sc.close();
			if(index != -1) {
				lines.set(index, lines.get(index).replaceFirst("空", studentAccommodation));
			}
			
			try{
				OutputStream fos = new FileOutputStream(f0);
			    OutputStreamWriter ior = new OutputStreamWriter(fos);
			    BufferedWriter br0 = new BufferedWriter(ior);
			    for(String l : lines) {
			    	 br0.write(l);
					 br0.newLine();
			    }
			    br0.close();
			}catch(Exception exp) {
				System.out.println(exp);
			}
			
			try{
				InputStream fos_ = new FileInputStream(f0);
				InputStreamReader ior_ = new InputStreamReader(fos_);
			    BufferedReader br0 = new BufferedReader(ior_);
				
				OutputStream fos = new FileOutputStream(f);
			    OutputStreamWriter ior = new OutputStreamWriter(fos);
			    BufferedWriter br3 = new BufferedWriter(ior);
			    String line_;
			    while ((line_ = br0.readLine()) != null) {
                    br3.write(line_);
                    br3.newLine();
                }
			    br3.close();
			}catch(Exception exp) {
				System.out.println(exp);
			}
			
			System.out.println("已安排床位，学生信息：" + studentAccommodation);
		}catch(Exception exp) {
			System.out.println(exp);
		}
	}
	
	
	
	public void educe() {
		System.out.println("* 导出学生住宿信息：*");
		File f = new File("D:\\计算机\\大二上\\文件\\Pro6\\src\\jieKeXiangMu\\宿舍资源.txt");
		try {
			FileInputStream fi = new FileInputStream(f);
		    InputStreamReader ir = new InputStreamReader(fi);
	        BufferedReader br = new BufferedReader(ir);
	        String line;
	        while((line=br.readLine())!=null) {
	        	System.out.println(line);
	        }
	        br.close();
	     }catch(Exception exp) {
				System.out.println(exp);
		 }
	}
	
	
	
	
	public void sanitation() {
		System.out.println("* 宿舍卫生情况（差）展示：*");
		System.out.println("提出警告！！！");
		File f = new File("D:\\计算机\\大二上\\文件\\Pro6\\src\\jieKeXiangMu\\宿舍资源.txt");
		String studentSanitation = "差";
		try {
			FileInputStream fi = new FileInputStream(f);
		    InputStreamReader ir = new InputStreamReader(fi);
	        BufferedReader br5 = new BufferedReader(ir);
	        String line;
	        while((line=br5.readLine())!=null) {
	        	if(line.contains(studentSanitation)) {
	        		System.out.println(line);
	        	}
	        }
	        br5.close();
	     }catch(Exception exp) {
				System.out.println(exp);
		 }
	}
}
